package sfeir.ergosum.adapter;

import java.util.ArrayList;

import sfeir.ergosum.R;
import sfeir.ergosum.model.Address;
import sfeir.ergosum.model.Email;
import sfeir.ergosum.model.Person;
import sfeir.ergosum.model.Tel;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailsListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<Object> details;
    private Person p;
    private ViewHolder holder;
    private boolean actionPhone = true;

    public DetailsListAdapter(Context c, Person p) {
        mContext = c;
        this.p = p;
        mInflater = LayoutInflater.from(mContext);
        details = new ArrayList<Object>();
        populate();
    }

    public void update(Person newP) {
        p = newP;
        details.clear();
        populate();
        notifyDataSetChanged();
    }

    private void populate() {
        details.add(p.getBirthDay());

        for (Email e : p.getEmails())
            details.add(e);
        for (Tel t : p.getTels())
            details.add(t);
        for (Address a : p.getAddresses())
            details.add(a);
    }

    private static class ViewHolder {
        ImageView logo;
        TextView info;
    }

    @Override
    public int getCount() {
        return details.size();
    }

    @Override
    public Object getItem(int position) {
        return details.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.logo = (ImageView) convertView
            .findViewById(R.id.list_item_image);
            holder.info = (TextView) convertView
            .findViewById(R.id.list_item_content);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Object deta = (Object) getItem(position);

        if (deta instanceof String) {
            if (null != deta) {
                holder.logo.setImageResource(R.drawable.date);
                holder.info.setText((String) deta);
            }
        } else if (deta instanceof Email && null != deta) {

            final String email = ((Email) deta).getEmail();
            final String[] mailto = { email };
            OnClickListener emailListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent sendIntent = new Intent(Intent.ACTION_SEND);
                    sendIntent.setData(Uri.parse("mailto:" + mailto));
                    sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
                    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Sujet");
                    sendIntent.setType("text/plain");
                    mContext.startActivity(Intent.createChooser(sendIntent,
                    "Choix du client mail"));
                }
            };

            holder.info.setText(email);
            convertView.setClickable(true);
            convertView.setOnClickListener(emailListener);

            switch (((Email) deta).getTypeEmail()) {

            case Email.PERSONAL_EMAIL:
                holder.logo.setImageResource(R.drawable.email_perso);
                break;
            case Email.PROFESSIONNAL_EMAIL:
                holder.logo.setImageResource(R.drawable.email_pro);
                break;
            case Email.UNKNOWN:
                holder.logo.setImageResource(R.drawable.email);
                break;
            default:
                holder.logo.setImageResource(R.drawable.email);
                break;
            }
        } else if (deta instanceof Tel && null != deta) {

            final String tel = (((Tel) deta).getPhoneNumber()).replace(" ", "");

            OnClickListener telListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(isActionPhone()) {
                        Intent iPhone =
                            new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tel));
                        mContext.startActivity(iPhone);
                    } else {
                        Intent iSms = 
                            new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + tel));
                        mContext.startActivity(iSms);
                    }
                }
            };

            holder.info.setText(tel);
            convertView.setClickable(true);
            convertView.setOnClickListener(telListener);

            switch (((Tel) deta).getType()) {

            case Tel.MOBILE_LINE:
                holder.logo.setImageResource(R.drawable.phone_mobile);
                break;
            case Tel.HOME_LANDLINE:
                holder.logo.setImageResource(R.drawable.phone_perso);
                break;
            case Tel.PROFESSIONNAL_LINE:
                holder.logo.setImageResource(R.drawable.phone_pro);
                break;
            default:
                holder.logo.setImageResource(R.drawable.phone_perso);
                break;
            }
        } else if (deta instanceof Address && null != deta) {
            final String addr = (((Address) deta)).getFullValue();

            OnClickListener addrListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri
                            .parse("geo:0,0?q=" + addr));
                    mContext.startActivity(i);
                }
            };

            holder.info.setText(addr);
            convertView.setClickable(true);
            convertView.setOnClickListener(addrListener);

            switch (((Address) deta).getType()) {

            case Address.PERSONAL_ADDRESS:
                holder.logo.setImageResource(R.drawable.house);
                break;
            case Address.PROFESSIONNAL_ADDRESS:
                holder.logo.setImageResource(R.drawable.work);
                break;
            case Address.DEFAULT_ADDRESS:
                holder.logo.setImageResource(R.drawable.house);
                break;
            default:
                holder.logo.setImageResource(R.drawable.house);
                break;
            }
        }
        return convertView;
    }

    public void setActionPhone(boolean actionPhone) {
        this.actionPhone = actionPhone;
    }

    public boolean isActionPhone() {
        return actionPhone;
    }
}
